<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Captcha;


class AuthController extends Controller
{
	/**
	 * 登录成功时跳转地址
	 * 
	 */
	protected $redirectPath = '/new';
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
    	$message = [
    		'required'    => ':attribute 是必填字段',
    		'size'           => ':attribute 手机号不是11位',
    		'email'        => ':attribute 邮箱格式不正确',
    		'password' => ':attribute 密码至少六位',
    	];
//     	dd($data);
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        	'mobile' => 'required|size:11',
        	'verify' => 'required',
        ],$message);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
    	//插入主表
        $user =  User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        	'mobile' => $data['mobile'],
        ]);
        
        //插入附加表 user_info
        //DB::table('user_info')->insert();
        
        return $user;
    }
    
    /*
     * 生成验证码
     */
    public function Verify()
    {
    	return Captcha::src();
    }
    
    /**
     * 验证码验证
     */
    public function VerifyCode($code)
    {
    	
    	return Captcha::check($code);
    }
}
